﻿using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Glimpse.Elmah.Infrastructure
{
	public class ErrorPage
	{
		private readonly int _pageIndex;
		private readonly int _pageSize;
		private readonly int _totalCount;
		private readonly IEnumerable<ErrorInfo> _errors;

		public ErrorPage(int pageIndex, int pageSize, int totalCount, IEnumerable<ErrorInfo> errors)
		{
			_pageIndex = pageIndex;
			_pageSize = pageSize;
			_totalCount = totalCount;
			_errors = errors;
		}

        public object GetData(HttpContextBase contextBase)
		{
			// Validate parameters
            if (contextBase == null)
				return null;

			// Validate state
			if (_errors == null ||
				_errors.Count() == 0)
			{
				return null;
			}

			// Create the header row
			var data = new List<object[]>
			           	{
			           		new object[]
			           			{
			           				"Host",
			           				"Code",
			           				"Type",
			           				"Error",
			           				"User",
			           				"Date",
			           				"Time",
			           				"Details"
			           			}
			           	};

			// Create the data rows
			data.AddRange(_errors.OrderByDescending(e => e.Date).Select(
                    errorEntry => new object[]
                                        {
                                            errorEntry.HostName,
                                            errorEntry.StatusCode,
                                            errorEntry.Type,
                                            errorEntry.Message,
                                            errorEntry.User,
                                            errorEntry.Date.ToShortDateString(),
                                            errorEntry.Date.ToShortTimeString(),
                                            string.Format("!{0}!", errorEntry.DetailsUrl)
                                        }));

			// Create the pager
            var path = VirtualPathUtility.ToAbsolute("~/", contextBase.Request.ApplicationPath);
			var pagerUrl = @"!<a href=""#"" onclick=""jQuery.glimpseElmah.loadPage({0}, {1})"" title=""{3}"">" +
						   @"<img src=""" + path + @"Glimpse.axd?r=Pager&resource=Pager{2}"" alt=""{3}"" />" +
						   @"</a>!";

			if (_pageIndex > 0)
			{
				// There are previous pages available
				var previousPageUrl = string.Format(pagerUrl, _pageIndex - 1, _pageSize, "Previous", "Previous page");
				data.Add(new object[] {previousPageUrl});

				var firstPageUrl = string.Format(pagerUrl, 0, _pageSize, "First", "First page");
				data.Add(new object[] {firstPageUrl});
			}
			
			var lastPageIndex = (_totalCount - 1)/_pageSize;
			if (_pageIndex < lastPageIndex)
			{
				// There are next pages left
				var nextPageUrl = string.Format(pagerUrl, _pageIndex + 1, _pageSize, "Next", "Next page");
				data.Add(new object[] {nextPageUrl});

                var lastPageUrl = string.Format(pagerUrl, lastPageIndex, _pageSize, "Last", "Last page");
				data.Add(new object[] {lastPageUrl});
			}

			return data;
		}
	}
}